using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Maps.MapControl;

namespace MapControlInteractiveSdk.Tutorials.UIElements
{
    public partial class HSMap : UserControl
    {
        private UIElement element = null;
        public HSMap()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Adds a UIElement
        /// </summary>
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            element = CreateUIElement();
            Location location = GetLocation();
            PositionOrigin position = GetPositionOrigin();
            MyLayer.AddChild(element, location, position);
            MapLayer.SetPositionOffset(element, GetOffset());
        }

        /// <summary>
        /// Change the Latlong position of the UIElement
        /// </summary>
        private void ChangePositionButton_Click(object sender, RoutedEventArgs e)
        {
            if (element != null)
            {
                Location location = GetLocation();
                MapLayer.SetPosition(element, location);
            }
        }

        /// <summary>
        /// Change the position method of the UIElement
        /// </summary>
        private void ChangePostionOriginButton_Click(object sender, RoutedEventArgs e)
        {
            if (element != null)
            {
                PositionOrigin position = GetPositionOrigin();
                MapLayer.SetPositionOrigin(element, position);
            }

        }

        /// <summary>
        /// Change the offset
        /// </summary>
        private void ChangePositionOffsetButton_Click(object sender, RoutedEventArgs e)
        {
            if (element != null)
            {
                Point point = GetOffset();
                MapLayer.SetPositionOffset(element, point);
            }
        }

        /// <summary>
        /// Remove all UIElement
        /// </summary>
        private void ChangeRemoveAllButton_Click(object sender, RoutedEventArgs e)
        {
            MyLayer.Children.Clear();
        }

        #region Private Helper Methods

        private UIElement CreateUIElement()
        {
            Canvas canvas = new Canvas();
            canvas.Background = new SolidColorBrush(Colors.White);
            canvas.Width = 100;
            canvas.Height = 100;

            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.Red);
            border.BorderThickness = new Thickness(1);
            border.Child = canvas;

            return border;
        }

        private PositionOrigin GetPositionOrigin()
        {
            PositionOrigin position = new PositionOrigin();

            if (PositionBottomCenterButton.IsChecked.Value)
            {
                position = PositionOrigin.BottomCenter;
            }
            else if (PositionBottomLeftButton.IsChecked.Value)
            {
                position = PositionOrigin.BottomLeft;
            }
            else if (PositionBottomRightButton.IsChecked.Value)
            {
                position = PositionOrigin.BottomRight;
            }
            else if (PositionCenterButton.IsChecked.Value)
            {
                position = PositionOrigin.Center;
            }
            else if (PositionNoneButton.IsChecked.Value)
            {
                position = new PositionOrigin();
            }
            else if (PositionTopCenterButton.IsChecked.Value)
            {
                position = PositionOrigin.TopCenter;
            }
            else if (PositionTopLeftButton.IsChecked.Value)
            {
                position = PositionOrigin.TopLeft;
            }
            else if (PositionTopRightButton.IsChecked.Value)
            {
                position = PositionOrigin.TopRight;
            }
            else if (CustomOriginButton.IsChecked.Value)
            {
                double x = 0.0;
                double y = 0.0;

                double.TryParse(XOriginValue.Text, out x);
                double.TryParse(YOriginValue.Text, out y);

                position = new PositionOrigin(x, y);
            }

            return position;
        }

        private Location GetLocation()
        {
            double lat = 0.0;
            double lon = 0.0;

            double.TryParse(LatValue.Text, out lat);
            double.TryParse(LonValue.Text, out lon);

            return new Location(lat, lon);
        }


        private Point GetOffset()
        {
            double x = 0.0;
            double y = 0.0;

            double.TryParse(XOffsetValue.Text, out x);
            double.TryParse(YOffsetValue.Text, out y);

            return new Point(x, y);
        }

        #endregion
    }
}
